<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
  <meta name="generator" content=
  "HTML Tidy for Linux/x86 (vers 1 September 2005), see www.w3.org">

  <title>C++ Operator Precedence</title>
  <link href="cppreference.css" rel="stylesheet" type="text/css">
</head>

<body>
<table>
  <tr>
  <td>
  <div class="body-content">

  <div class="header-box">
    <a href="index.html">cppreference.com</a> &gt; C++ Operator
    Precedence
  </div>

  <h3>C++ Operator Precedence</h3>

  <p>The operators at the top of this list are evaluated first.</p>

  <table class="code-table">
    <tr>
      <th class="code-table-th">Precedence</th>

      <th class="code-table-th">Operator</th>

      <th class="code-table-th">Description</th>

      <th class="code-table-th">Example</th>

      <th class="code-table-th">Associativity</th>
    </tr>

    <tr>
      <td class="code-table-td">1</td>

      <td class="code-table-td">
      ::<br>

      <td class="code-table-td">
      Scoping operator<br>

      <td class="code-table-td">
      Class::age = 2;<br>

      <td class="code-table-td">none</td>
    </tr>

    <tr>
      <td class="code-table-td">2</td>

      <td class="code-table-td">()<br>
      []<br>
      -&gt;<br>
      .<br>
      ++<br>
      --</td>

      <td class="code-table-td">Grouping operator<br>
      Array access<br>
      Member access from a pointer<br>
      Member access from an object<br>
      Post-increment<br>
      Post-decrement</td>

      <td class="code-table-td">(a + b) / 4;<br>
      array[4] = 2;<br>
      ptr-&gt;age = 34;<br>
      obj.age = 34;<br>
      for( i = 0; i &lt; 10; i++ ) ...<br>
      for( i = 10; i &gt; 0; i-- ) ...</td>

      <td class="code-table-td">left to right</td>
    </tr>

    <tr>
      <td class="code-table-td">3</td>

      <td class="code-table-td">!<br>
      ~<br>
      ++<br>
      --<br>
      -<br>
      +<br>
      *<br>
      &amp;<br>
      (type)<br>
      <a href="keywords/sizeof.html">sizeof</a></td>

      <td class="code-table-td">Logical negation<br>
      Bitwise complement<br>
      Pre-increment<br>
      Pre-decrement<br>
      Unary minus<br>
      Unary plus<br>
      Dereference<br>
      Address of<br>
      Cast to a given type<br>
      Return size in bytes</td>

      <td class="code-table-td">if( !done ) ...<br>
      flags = ~flags;<br>
      for( i = 0; i &lt; 10; ++i ) ...<br>
      for( i = 10; i &gt; 0; --i ) ...<br>
      int i = -1;<br>
      int i = +1;<br>
      data = *ptr;<br>
      address = &amp;obj;<br>
      int i = (int) floatNum;<br>
      int size = sizeof(floatNum);</td>

      <td class="code-table-td">right to left</td>
    </tr>

    <tr>
      <td class="code-table-td">4</td>

      <td class="code-table-td">-&gt;*<br>
      .*</td>

      <td class="code-table-td">Member pointer selector<br>
      Member object selector</td>

      <td class="code-table-td">ptr-&gt;*var = 24;<br>
      obj.*var = 24;</td>

      <td class="code-table-td">left to right</td>
    </tr>

    <tr>
      <td class="code-table-td">5</td>

      <td class="code-table-td">*<br>
      /<br>
      %</td>

      <td class="code-table-td">Multiplication<br>
      Division<br>
      Modulus</td>

      <td class="code-table-td">int i = 2 * 4;<br>
      float f = 10 / 3;<br>
      int rem = 4 % 3;</td>

      <td class="code-table-td">left to right</td>
    </tr>

    <tr>
      <td class="code-table-td">6</td>

      <td class="code-table-td">+<br>
      -</td>

      <td class="code-table-td">Addition<br>
      Subtraction</td>

      <td class="code-table-td">int i = 2 + 3;<br>
      int i = 5 - 1;</td>

      <td class="code-table-td">left to right</td>
    </tr>

    <tr>
      <td class="code-table-td">7</td>

      <td class="code-table-td">&lt;&lt;<br>
      &gt;&gt;</td>

      <td class="code-table-td">Bitwise shift left<br>
      Bitwise shift right</td>

      <td class="code-table-td">int flags = 33 &lt;&lt; 1;<br>
      int flags = 33 &gt;&gt; 1;</td>

      <td class="code-table-td">left to right</td>
    </tr>

    <tr>
      <td class="code-table-td">8</td>

      <td class="code-table-td">&lt;<br>
      &lt;=<br>
      &gt;<br>
      &gt;=</td>

      <td class="code-table-td">Comparison less-than<br>
      Comparison less-than-or-equal-to<br>
      Comparison greater-than<br>
      Comparison geater-than-or-equal-to</td>

      <td class="code-table-td">if( i &lt; 42 ) ...<br>
      if( i &lt;= 42 ) ...<br>
      if( i &gt; 42 ) ...<br>
      if( i &gt;= 42 ) ...</td>

      <td class="code-table-td">left to right</td>
    </tr>

    <tr>
      <td class="code-table-td">9</td>

      <td class="code-table-td">==<br>
      !=</td>

      <td class="code-table-td">Comparison equal-to<br>
      Comparison not-equal-to</td>

      <td class="code-table-td">if( i == 42 ) ...<br>
      if( i != 42 ) ...</td>

      <td class="code-table-td">left to right</td>
    </tr>

    <tr>
      <td class="code-table-td">10</td>

      <td class="code-table-td">&amp;</td>

      <td class="code-table-td">Bitwise AND</td>

      <td class="code-table-td">flags = flags &amp; 42;</td>

      <td class="code-table-td">left to right</td>
    </tr>

    <tr>
      <td class="code-table-td">11</td>

      <td class="code-table-td">^</td>

      <td class="code-table-td">Bitwise exclusive OR</td>

      <td class="code-table-td">flags = flags ^ 42;</td>

      <td class="code-table-td">left to right</td>
    </tr>

    <tr>
      <td class="code-table-td">12</td>

      <td class="code-table-td">|</td>

      <td class="code-table-td">Bitwise inclusive (normal) OR</td>

      <td class="code-table-td">flags = flags | 42;</td>

      <td class="code-table-td">left to right</td>
    </tr>

    <tr>
      <td class="code-table-td">13</td>

      <td class="code-table-td">&amp;&amp;</td>

      <td class="code-table-td">Logical AND</td>

      <td class="code-table-td">if( conditionA &amp;&amp; conditionB ) ...</td>

      <td class="code-table-td">left to right</td>
    </tr>

    <tr>
      <td class="code-table-td">14</td>

      <td class="code-table-td">||</td>

      <td class="code-table-td">Logical OR</td>

      <td class="code-table-td">if( conditionA || conditionB ) ...</td>

      <td class="code-table-td">left to right</td>
    </tr>

    <tr>
      <td class="code-table-td">15</td>

      <td class="code-table-td">? :</td>

      <td class="code-table-td">Ternary conditional (if-then-else)</td>

      <td class="code-table-td">int i = (a &gt; b) ? a : b;</td>

      <td class="code-table-td">right to left</td>
    </tr>

    <tr>
      <td class="code-table-td">16</td>

      <td class="code-table-td">=<br>
      +=<br>
      -=<br>
      *=<br>
      /=<br>
      %=<br>
      &amp;=<br>
      ^=<br>
      |=<br>
      &lt;&lt;=<br>
      &gt;&gt;=</td>

      <td class="code-table-td">Assignment operator<br>
      Increment and assign<br>
      Decrement and assign<br>
      Multiply and assign<br>
      Divide and assign<br>
      Modulo and assign<br>
      Bitwise AND and assign<br>
      Bitwise exclusive OR and assign<br>
      Bitwise inclusive (normal) OR and assign<br>
      Bitwise shift left and assign<br>
      Bitwise shift right and assign</td>

      <td class="code-table-td">int a = b;<br>
      a += 3;<br>
      b -= 4;<br>
      a *= 5;<br>
      a /= 2;<br>
      a %= 3;<br>
      flags &amp;= new_flags;<br>
      flags ^= new_flags;<br>
      flags |= new_flags;<br>
      flags &lt;&lt;= 2;<br>
      flags &gt;&gt;= 2;</td>

      <td class="code-table-td">right to left</td>
    </tr>

    <tr>
      <td class="code-table-td">17</td>

      <td class="code-table-td">,</td>

      <td class="code-table-td">Sequential evaluation operator</td>

      <td class="code-table-td">for( i = 0, j = 0; i &lt; 10; i++, j++
      ) ...</td>

      <td class="code-table-td">left to right</td>
    </tr>
  </table>

  <p>It is important to note that <strong>there is no specified
  precedence</strong> for the operation of changing a variable into a
  value.  For example, consider the following code:</p>

  <pre class="example-code">
  float x, result;
  x = 1;
  result = x / ++x;
</pre>

  <p>The value of <it>result</it> is not guaranteed to be
  consistent across different compilers, because it is not clear
  whether the computer should change the variable <it>x</it> 
  (the one that occurs on the left side of the division operator) 
  before using it.  Depending on which compiler you are using, the
  variable <it>result</it> can either be <strong>1.0</strong> or 
  <strong>0.5</strong>.  The bottom line is that you <strong>should not use
  the same variable multiple times in a single expression when using
  operators with side effects.</strong></p>

  </div>
  </td>
  



  </tr>
  </table>
</body></html>
